package jmine.tec.hydra.actions.services.incluir;

import jmine.tec.di.annotation.Injected;
import jmine.tec.hydra.actions.domain.MockAuthorizableEntity;
import jmine.tec.services.annotations.Execution;
import jmine.tec.services.annotations.Input;
import jmine.tec.services.annotations.ServiceImplementor;
import jmine.tec.services.annotations.Validation;
import jmine.tec.services.api.ActionsEnum;
import jmine.tec.services.api.ServiceExecutionException;
import jmine.tec.services.api.ServiceValidationExecutionException;
import bancosys.tec.exception.MessageHolder;
import bancosys.tec.persist.dao.BaseDAO;
import bancosys.tec.persist.validator.ValidationError;

@ServiceImplementor(action = ActionsEnum.INCLUIR)
public class MockAuthorizableEntityService {

    public static final String MNEMONICO = "Mnemônico";

    public static final String SERVICE_RUNTIME_EXCEPTION = "Service runtime exception";

    public static final String BUNDLE_NAME = "Bundle name";

    public static final String VALIDATION_ERROR = "Validation error";

    public static final String VALIDATION_EXCEPTION = "Validation exception";

    public static final String SERVICE_EXECUTION_EXCEPTION = "Service execution exception";

    public static final String SERVICE_VALIDATION_EXECUTION_EXCEPTION = "Service validation execution exception";

    // Injected
    private BaseDAO<MockAuthorizableEntity> dao;

    // Obrigatório
    private String mnemonico;

    // Opcionais
    private String serviceRuntimeException;

    private String bundleName;

    private String validationError;

    private String validationException;

    private String serviceExecutionException;

    private String serviceValidationExecutionException;

    @Execution
    public void execute() throws ServiceExecutionException {
        if (this.bundleName != null) {
            if (this.serviceExecutionException != null) {
                throw new ServiceExecutionException(new MessageHolder(this.bundleName, this.serviceExecutionException));
            }
            if (this.serviceRuntimeException != null) {
                throw new RuntimeException(this.serviceRuntimeException);
            }
        }
        MockAuthorizableEntity entity = this.dao.createBean();
        entity.setMnemonico(this.mnemonico);
        if (this.validationException != null) {
            entity.setValidationException(new RuntimeException(this.validationException));
        }
        if (this.bundleName != null && this.validationError != null) {
            entity.setValidationError(new ValidationError(new MessageHolder(this.bundleName, this.validationError)));
        }
        entity.getPersister().save();
    }

    @Validation
    public void validate() {
        if (this.bundleName != null && this.serviceValidationExecutionException != null) {
            throw new ServiceValidationExecutionException(new MessageHolder(this.bundleName, this.serviceValidationExecutionException));
        }
    }

    /**
     * @param dao the dao to set
     */
    @Injected
    public void setDao(BaseDAO<MockAuthorizableEntity> dao) {
        this.dao = dao;
    }

    /**
     * @param mnemonico the mnemonico to set
     */
    @Input(fieldName = MNEMONICO)
    public void setMnemonico(String mnemonico) {
        this.mnemonico = mnemonico;
    }

    /**
     * @param serviceRuntimeException the serviceRuntimeException to set
     */
    @Input(fieldName = SERVICE_RUNTIME_EXCEPTION, required = false)
    public void setServiceRuntimeException(String serviceRuntimeException) {
        this.serviceRuntimeException = serviceRuntimeException;
    }

    /**
     * @param bundleName the bundleName to set
     */
    @Input(fieldName = BUNDLE_NAME, required = false)
    public void setBundleName(String bundleName) {
        this.bundleName = bundleName;
    }

    /**
     * @param validationError the validationError to set
     */
    @Input(fieldName = VALIDATION_ERROR, required = false)
    public void setValidationError(String validationError) {
        this.validationError = validationError;
    }

    /**
     * @param validationException the validationException to set
     */
    @Input(fieldName = VALIDATION_EXCEPTION, required = false)
    public void setValidationException(String validationException) {
        this.validationException = validationException;
    }

    /**
     * @param serviceExecutionException the serviceExecutionException to set
     */
    @Input(fieldName = SERVICE_EXECUTION_EXCEPTION, required = false)
    public void setServiceExecutionException(String serviceExecutionException) {
        this.serviceExecutionException = serviceExecutionException;
    }

    /**
     * @param serviceValidationExecutionException the serviceValidationExecutionException to set
     */
    @Input(fieldName = SERVICE_VALIDATION_EXECUTION_EXCEPTION, required = false)
    public void setServiceValidationExecutionException(String serviceValidationExecutionException) {
        this.serviceValidationExecutionException = serviceValidationExecutionException;
    }
}
